package com.youlu.campus.web.complaint;


import com.youlu.campus.wechat.service.impl.MerchantComplaintService;
import com.youlu.campus.service.config.OuterAppConfigService;
import com.youlu.campus.service.mail.MailService;
import com.youlu.campus.web.common.utils.CommonUtils;
import com.yuelin.infrastructure.quantum.common.QResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * 微信商户投诉接口
 *
 */
@Slf4j
@RestController
public class MerchantComplaintController {

    @Resource
    private MerchantComplaintService merchantComplaintService;

    @Resource
    private MailService mailService;
    @Resource
    private OuterAppConfigService outerAppConfigService;


    /**
     * 商户号通知投诉处理
     *
     * @param request
     * @return
     */
    @PostMapping("/public/complaint-notify")
    public Map<String, Object> complaintNotify(HttpServletRequest request, HttpServletResponse response) {
        return merchantComplaintService.complaintNotify(request);
    }

    /**
     * 主动查询投诉列表
     *
     * @param merchantId 商户id
     * @param begin_date 开始时间 yyyy-MM-dd
     * @param end_date 结束时间 yyyy-MM-dd
     * @param limit 显示条数
     * @param offset 开始条数
     * @return
     */
    @GetMapping("public/complaint/list")
    public QResult complaintList(@RequestParam String merchantId,
                                 @RequestParam(required = false, defaultValue = "2025-01-01") String begin_date,
                                 @RequestParam(required = false, defaultValue = "2025-07-03") String end_date,
                                 @RequestParam(required = false, defaultValue = "10") Integer limit,
                                 @RequestParam(required = false, defaultValue = "0") Integer offset
    ) {
        HashMap complaintlist = merchantComplaintService.complaintlist(merchantId, begin_date, end_date, limit, offset);
        return QResult.success(complaintlist);
    }

    /**
     * 查询投诉单详情
     *
     * @param merchantId
     * @param complaintId
     * @return
     */
    @GetMapping("public/complaint/detail")
    public QResult detail(@RequestParam String merchantId, @RequestParam("complaintId") String complaintId) {
        merchantComplaintService.remoteDetail(merchantId, complaintId);
        return QResult.success(null);
    }

    /*@GetMapping("public/complaint/signature")
    public QResult signature(@RequestParam String merchantId) {
        merchantComplaintService.testSignature(merchantId);
        return QResult.success(null);
    }*/

    /**
     * 创建商户通知
     *
     * @param merchantId
     * @return
     */
    @GetMapping("/public/complaint/create-notify")
    public QResult createNotify(HttpServletRequest request, @RequestParam String merchantId, @RequestParam String notifyUrl) {
        String ip = CommonUtils.getRemoteIp(request);
        String url = request.getRequestURI();
        String method = request.getMethod();
        String domain=request.getServerName();
        int port=request.getRemotePort();
        log.info(":>>> 请求的URL:{},方法:{},域名:{},远程端口:{}", url,method,domain,port);
        boolean pass = outerAppConfigService.check(ip, domain, "rd");
        if (!pass) {
            log.error(":>>> IP 地址校验失败:{},with url:{}", ip, "/public/complaint/create-notify");
            return QResult.fail("非法IP,系统已经告警，请自重！");
        }
        merchantComplaintService.doSyncNotifyUrl(merchantId, notifyUrl);
        return QResult.success(null);
    }

    /**
     * 删除商户通知
     *
     * @param merchantId
     * @return
     */
    @GetMapping("/public/complaint/delete-notify")
    public QResult deleteNotify(HttpServletRequest request, @RequestParam String merchantId) {
        merchantComplaintService.doDeleteNotifyUrl(merchantId);
        return QResult.success(null);
    }

    /**
     * 查询商户通知
     *
     * @param merchantId
     * @return
     */
    @GetMapping("/public/complaint/query-notify")
    public QResult queryNotify(HttpServletRequest request, @RequestParam String merchantId) {
        return QResult.success(merchantComplaintService.doQueryNotifyUrl(merchantId));
    }

    @GetMapping("/public/mail/send")
    public QResult sendMail() {
        mailService.sendMail();
        return QResult.success(null);
    }

    /**
     * 初始化商户号对比是否在投诉表中
     *
     * @return
     */
    @GetMapping("/public/complaint/compare")
    public QResult initCompare() {
        merchantComplaintService.initCompare();
        return QResult.success(null);
    }

}
